//
//  WHHomeTableViewController.swift
//  WHSinaWeiBo
//
//  Created by 王寒 on 16/4/6.
//  Copyright © 2016年 wanghan. All rights reserved.
//

import UIKit
import SVProgressHUD

private let homeCellID = "HomeCellid"

class WHHomeTableViewController: WHVisTorTableViewController {

    
    lazy var statusListViewModel: WHStatusLiskViewModel = WHStatusLiskViewModel()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        //设置界面
        stepUI()
        //加载数据
        loadData()
    }
    
    //设置界面
    func stepUI(){
        if !userLogin{
            visitorView?.setVisitorInfo("visitordiscover_feed_image_smallicon",title: "关注一些人,回到这里看看有什么动态",isHome: true)
            
            return
        }
        navigationItem.rightBarButtonItem = UIBarButtonItem(imageName: "navigationbar_pop",title: nil, target: self, action: "rightBarDidClick")
        
        //注册cell
        prepareForTableView()
        
       //设置自适应高度
        tableView.rowHeight = UITableViewAutomaticDimension
        //设置预估行高
        tableView.estimatedRowHeight = 300
        //取消分割线
        tableView.separatorStyle = .None
        

    }
    
    //注册cell
    func prepareForTableView(){
        tableView.registerClass(WHStatusCell.self, forCellReuseIdentifier:homeCellID)
    }
    
    //根据code获取用户数据
    func loadData(){
        
        statusListViewModel.loadData { (isSuccess) -> () in
            
            if !isSuccess {
                //数据错误
                SVProgressHUD.showErrorWithStatus(AppErrorTip)
                return
            }
            
            //获取数据成功
            //刷新表格
            self.tableView.reloadData()
        }
    }
    
    
    
    @objc private func rightBarDidClick(){
        
        let rightVc = WHTempViewController()

        navigationController?.pushViewController(rightVc, animated: true)
    }
    
}

//MARK:数据源fangfa
extension WHHomeTableViewController{
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.statusListViewModel.statusArr.count
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        let cell = tableView .dequeueReusableCellWithIdentifier(homeCellID, forIndexPath: indexPath) as! WHStatusCell
        
//        cell.textLabel?.text = statusListViewModel.status[indexPath.row].text
        cell.statusViewModel = statusListViewModel.statusArr[indexPath.row]
        
        return cell
    }
    
}
